﻿using System.Windows.Documents;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Markup;
using System;
using System.Linq;
using WiseLinkWPF.Windows.Controls.GridView;
using WiseLinkWPF.Windows.Controls;
using System.Printing;
using GridViewColumn = WiseLinkWPF.Windows.Controls.GridViewColumn;
using System.Collections.Generic;
using System.Collections;
using CCF20Model;

namespace CCF20Module
{
    public static class PrintExtensions
    {
        public static FixedDocument ToFixedDocument(FrameworkElement element, PrintDialog dialog)
        {
            PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
            Size pageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight);
            Size extentSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

            FixedDocument fixedDocument = new FixedDocument();

            element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            element.Arrange(new Rect(new Point(0, 0), element.DesiredSize));

            double totalHeight = element.DesiredSize.Height;

            double yOffset = 0d;
            while (yOffset < totalHeight)
            {
                VisualBrush brush = new VisualBrush(element);
                brush.Stretch = Stretch.None;
                brush.AlignmentX = AlignmentX.Left;
                brush.AlignmentY = AlignmentY.Top;
                brush.ViewboxUnits = BrushMappingMode.Absolute;
                brush.TileMode = TileMode.None;
                brush.Viewbox = new Rect(0, yOffset, extentSize.Width, extentSize.Height);


                PageContent pageContent = new PageContent();
                FixedPage page = new FixedPage();
                ((IAddChild)pageContent).AddChild(page);

                fixedDocument.Pages.Add(pageContent);
                page.Width = pageSize.Width;
                page.Height = pageSize.Height;

                Canvas canvas = new Canvas();
                FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
                FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight);
                canvas.Width = extentSize.Width;
                canvas.Height = extentSize.Height;
                canvas.Background = brush;

                //Grid titleGrid = new Grid();
                //TextBlock tb = new TextBlock();
                //tb.Text = "Hello";
                //tb.HorizontalAlignment = HorizontalAlignment.Center;
                
                //titleGrid.Children.Add(tb);

                //page.Children.Add(titleGrid);
                page.Children.Add(canvas);

                yOffset += extentSize.Height;
            }
            return fixedDocument;
        }

        public static GridViewDataControl ToPrintFriendlyGrid(GridViewDataControl source,RadGridView grid )
        {
            grid.ItemsSource = source.ItemsSource;
            grid.RowIndicatorVisibility = Visibility.Collapsed;
            grid.ShowGroupPanel = false;
            grid.CanUserFreezeColumns = false;
            grid.IsFilteringAllowed = false;
            grid.AutoExpandGroups = true;
            grid.AutoGenerateColumns = false;
            grid.HorizontalAlignment = HorizontalAlignment.Stretch;

            grid.RowStyle = source.RowStyle;
            grid.RowStyleSelector = source.RowStyleSelector;
            grid.AlternationCount = source.AlternationCount;
            grid.AlternateRowBackground = source.AlternateRowBackground;
            grid.AlternateRowStyle = source.AlternateRowStyle;
            grid.AlternateRowStyleSelector = source.AlternateRowStyleSelector;
            grid.ColumnBackground = source.ColumnBackground;

            grid.SelectedItems.Clear();

            foreach (GridViewColumn column in source.Columns.OfType<GridViewColumn>())
            {
                Type columnType = column.GetType();
                GridViewColumn newColumn = (GridViewColumn)Activator.CreateInstance(columnType);
                newColumn.CopyPropertiesFrom(column);
                grid.Columns.Add(newColumn);
            }
            grid.CalculateAggregates();
            StyleManager.SetTheme(grid, StyleManager.GetTheme(grid));

            grid.SortDescriptors.AddRange(source.SortDescriptors);
            grid.GroupDescriptors.AddRange(source.GroupDescriptors);
            grid.FilterDescriptors.AddRange(source.FilterDescriptors);

            return grid;
        }

        //public static void PrintPreview<T>(GridViewDataControl source, T data)
        //{
        //    Window window = new Window();
        //    window.Title = "Print Preview";

        //    DocumentViewer documentViewer = new DocumentViewer();
        //    documentViewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source, data), new PrintDialog());
        //    window.Content = documentViewer;

        //    window.ShowDialog();
        //}

        //public static void Print<T>(GridViewDataControl source, bool showDialog, T data)
        //{
        //    PrintDialog dialog = new PrintDialog();
        //    bool? dialogResult = showDialog ? dialog.ShowDialog() : true;

        //    if (dialogResult == true)
        //    {
        //        DocumentViewer viewer = new DocumentViewer();
        //        viewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source, data), dialog);
        //        dialog.PrintDocument(viewer.Document.DocumentPaginator, null);
        //    }
        //}
    }
}